/**
 * Copyright (c) 2018-2028, HQY 胡庆一 (215218393@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocm.pay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.ocm.core.boot.tenant.TenantConstant;
import com.ocm.core.mp.support.Condition;
import com.ocm.core.mp.support.Query;
import com.ocm.core.secure.BladeUser;
import com.ocm.core.tool.api.R;
import com.ocm.core.tool.utils.BeanUtil;
import com.ocm.core.tool.utils.Func;
import com.ocm.pay.dto.BankcardDTO;
import com.ocm.pay.entity.ShopPayinfo;
import com.ocm.pay.entity.UserPayinfo;
import com.ocm.pay.service.IShopPayinfoService;
import com.ocm.pay.service.IUserPayinfoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocm.pay.entity.Bankcard;
import com.ocm.pay.vo.BankcardVO;
import com.ocm.pay.wrapper.BankcardWrapper;
import com.ocm.pay.service.IBankcardService;
import com.ocm.core.boot.ctrl.BladeController;

import java.util.List;

/**
 *  控制器
 *
 * @author HQY
 * @since 2021-12-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/bankcard")
@Api(value = "", tags = "接口")
public class BankcardController extends BladeController {

	private final IBankcardService bankcardService;


	private final IUserPayinfoService userPayinfoService ;

	private final IShopPayinfoService shopPayinfoService ;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入bankcard")
	public R<BankcardVO> detail(Bankcard bankcard) {
		Bankcard detail = bankcardService.getOne(Condition.getQueryWrapper(bankcard));
		return R.data(BankcardWrapper.build().entityVO(detail));
	}


	/**
	 * 银行卡列表
	 */
	@GetMapping("/u/bankcard")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "银行卡列表", notes = "传入bankcard")
	public R<List<BankcardVO>> uBankcardList(BladeUser bladeUser) {
		List<BankcardVO> list = bankcardService.bankcardList(bladeUser.getUserId());
		return R.data(list);
	}

	/**
	 * 绑卡
	 */
	@PostMapping("/u/bankcard")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入bankcard")
	public R<Boolean> bankcardList(@Valid @RequestBody BankcardDTO dto,BladeUser bladeuser) {
		//todo 验证码校验请对接银行接口
		if(!"111111".equals(dto.getCode())){
			return R.fail("验证码错误");
		}
		//判断银行卡状态
		if(bankcardService.count(new QueryWrapper<Bankcard>()
			.eq("tenant_id",dto.getTenantId())
			.eq("bank_card",dto.getBankCard())
			.eq("is_deleted",0))>0){
			return R.fail("此卡已被关联,如有问题,请联系客服");
		}
		Bankcard copy = BeanUtil.copy(dto, Bankcard.class);
		//租户判断
		if(TenantConstant.USER_TENANT_ID.equals(dto.getTenantId())){
			UserPayinfo user = userPayinfoService.getOne(
				new QueryWrapper<UserPayinfo>().eq("user_id",bladeuser.getUserId()).eq("is_deleted",0));
			copy.setBankName(user.getCodeName());
		}else if(TenantConstant.SHOP_TENANT_ID.equals(dto.getTenantId())){
			ShopPayinfo shopPayinfo = shopPayinfoService.getOne(
				new QueryWrapper<ShopPayinfo>().eq("shop_id",bladeuser.getShopId()).eq("is_deleted",0));
			copy.setBankName(shopPayinfo.getCodeName());
		}

		copy.setUserId(bladeuser.getUserId());
		return R.status(bankcardService.save(copy));
	}


	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入bankcard")
	public R<IPage<BankcardVO>> list(Bankcard bankcard, Query query) {
		IPage<Bankcard> pages = bankcardService.page(Condition.getPage(query), Condition.getQueryWrapper(bankcard));
		return R.data(BankcardWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入bankcard")
	public R<IPage<BankcardVO>> page(BankcardVO bankcard, Query query) {
		IPage<BankcardVO> pages = bankcardService.selectBankcardPage(Condition.getPage(query), bankcard);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入bankcard")
	public R save(@Valid @RequestBody Bankcard bankcard) {
		return R.status(bankcardService.save(bankcard));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入bankcard")
	public R update(@Valid @RequestBody Bankcard bankcard) {
		return R.status(bankcardService.updateById(bankcard));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入bankcard")
	public R submit(@Valid @RequestBody Bankcard bankcard) {
		return R.status(bankcardService.saveOrUpdate(bankcard));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(bankcardService.deleteLogic(Func.toLongList(ids)));
	}


}
